﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace HenIT.CAGCompare
{
    public partial class CompareFiles : Form
    {
        public CompareFiles()
        {
            InitializeComponent();
        }

        public List<string> SelectedFiles { get; set; }

        private void CompareFiles_Load(object sender, EventArgs e)
        {
            if (SelectedFiles != null && SelectedFiles.Count > 0)
            {
                lvwFiles.Items.Clear();
                foreach (string filePath in SelectedFiles)
                {
                    try
                    {
                        ListViewItem lvi = new ListViewItem(filePath);
                        FileInfo fi = new FileInfo(filePath);
                        if (!fi.Exists)
                        {
                            lvi.BackColor = Color.Coral;
                            lvi.SubItems.Add("N/A");
                            lvi.SubItems.Add("N/A");
                        }
                        else
                        {
                            lvi.SubItems.Add(fi.LastWriteTime.ToString("yyyy-MM-dd HH:mm:ss"));
                            lvi.SubItems.Add((fi.Length / 1024) + "KB");
                        }
                        lvwFiles.Items.Add(lvi);
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }
                CompareFiles_Resize(null, null);
            }
        }

        private void CompareFiles_Resize(object sender, EventArgs e)
        {
            lvwFiles.Columns[0].Width = lvwFiles.ClientSize.Width - lvwFiles.Columns[1].Width - lvwFiles.Columns[2].Width;
        }

        private void lvwFiles_SelectedIndexChanged(object sender, EventArgs e)
        {
            copyPathToClipboardToolStripMenuItem.Enabled = lvwFiles.SelectedItems.Count == 1;
        }

        private void copyPathToClipboardToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (lvwFiles.SelectedItems.Count == 1)
            {
                Clipboard.SetText(lvwFiles.SelectedItems[0].Text);
            }
        }

        private void refreshToolStripMenuItem_Click(object sender, EventArgs e)
        {
            foreach (ListViewItem lvi in lvwFiles.Items)
            {
                try
                {
                    FileInfo fi = new FileInfo(lvi.Text);
                    if (!fi.Exists)
                    {
                        lvi.BackColor = Color.Coral;
                        lvi.SubItems[1].Text = "N/A";
                        lvi.SubItems[2].Text = "N/A";
                    }
                    else
                    {
                        lvi.BackColor = SystemColors.Window;
                        lvi.SubItems[1].Text = fi.LastWriteTime.ToString("yyyy-MM-dd HH:mm:ss");
                        lvi.SubItems[2].Text = ((fi.Length / 1024) + "KB");
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }

    }
}
